package com.hzp.micromall.content.controller;


import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.common.core.api.CommonResult;
import com.hzp.micromall.content.entity.PrefrenceArea;
import com.hzp.micromall.content.service.PrefrenceAreaService;
import com.hzp.micromall.content.vo.QueryParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优选专区 前端控制器
 * </p>
 *
 * @author houzipeng
 * @since 2022-03-08 10:15:14
 */
@RestController
@RequestMapping("/content/prefrenceArea")
public class PrefrenceAreaController {
    @Autowired
    private PrefrenceAreaService prefrenceAreaService;

    @ApiOperation("添加优选专区")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createArea(@RequestBody PrefrenceArea area) {
        int count = prefrenceAreaService.create(area);
        if (count > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取所有商品优选")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PrefrenceArea>> listAll() {
        List<PrefrenceArea> prefrenceAreaList = prefrenceAreaService.listAll();
        return CommonResult.success(prefrenceAreaList);
    }

    @ApiOperation("分页获取优选专区信息")
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "showStatus", required = false) Integer showStatus,
                                @RequestParam(value = "startTime", required = false) String startTime,
                                @RequestParam(value = "endTime", required = false) String endTime,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        CommonPage<PrefrenceArea> prefrenceAreaPage = prefrenceAreaService.getPage(keyword, showStatus, startTime, endTime, pageNum, pageSize);
        return CommonResult.success(prefrenceAreaPage);
    }

//    @ApiOperation("分页获取优选专区信息")
//    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult getPage(@RequestBody QueryParams params) {
//        CommonPage<PrefrenceArea> prefrenceAreaPage = prefrenceAreaService.getPage(params);
//        return CommonResult.success(prefrenceAreaPage);
//    }

    @ApiOperation("修改优选专区信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id,
                                @RequestBody PrefrenceArea prefrenceArea) {
        int count = prefrenceAreaService.update(id, prefrenceArea);
        if (count > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改优选专区显示状态")
    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateShowStatus(@RequestParam("ids") List<Long> ids,
                               @RequestParam("showStatus") Integer newShowStatus) {
        int count = prefrenceAreaService.updateShowStatus(ids, newShowStatus);
        if (count > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除优选专区")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = prefrenceAreaService.delete(ids);
        if (count > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}

